package com.zlm.tools.validation;

import net.sf.jsqlparser.util.validation.ValidationException;

import java.util.ArrayList;
import java.util.List;

/**
 * {@code 校验上下文}
 *
 * @author zlm
 * @since 2025/5/30
 */
public class ValidatorContext {

    private final List<String> errors = new ArrayList<>();

    /**
     * 是否停止校验
     */
    private boolean stop =  false;
    /**
     * 当前责任链的位置
     */
    private int index = 0;

    private Object value;


    public ValidatorContext(Object value) {
        this.value = value;
    }


    /**
     * 添加错误信息
     *
     * @param error 错误信息
     */
    public void addError(String error) {
        errors.add(error);
    }

    public void stopChain(){
        this.stop = true;
    }

    public boolean shouldStop(){
        return stop;
    }


    /**
     * 如果有错误信息，则抛出异常
     */
    public void throwExceptionIfNecessary(){
        if (errors.isEmpty()) {
            return;
        }
        throw new ValidationException(errors.toString());
    }

    public void doNext(Object value) {
        index++;
        this.value = value;
    }

    public int currentIndex(){
        return index;
    }
    public Object getValue(){
        return value;
    }
}
